﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Speech.Recognition;

namespace voiceRec
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // when adding a new commad:
        //
        //        - increment the size of words_copy
        //        - add an "if" statement in void _recognizer_SpeechRecognized(..)
        //        - if required, show confirmation and add to switch in confirmationWindow class

        public bool voiceCommandsActive;
        public string[] words = { "logoff", "shutdown", "restart", "Hibernate", "deactivate", "activate", "firefox", "internet explorer" };
        public string[] words_copy = new string[8];
        public double conf = 0.9;
        public const string IniFile = @"..\..\IniFiles\Config.ini";

        bool firefox = true;
        bool explorer = true;
        bool _restart = true;
        bool _shutdown = true;
        bool _logoff = true;
        bool _Hibernate = true;

        confirmationWindow confirmation = new confirmationWindow();
        private SpeechRecognitionEngine _recognizer;

        public MainWindow()
        {
            InitializeComponent();
            textBox1.Text = "deactivated";
        }
        private void ButtonRecognizeVoice_Click(object sender, RoutedEventArgs e)
        {
            if (textBox2.Text == "")
            {
                textBox2.Text = "kinect";
            }

            voiceCommandsActive = true;

            IniFile ini = new IniFile(IniFile);
            ini.IniWriteValue("voice control", "key word", textBox2.Text);

            textBox1.Text = "activated";
            _recognizer = new SpeechRecognitionEngine();
            _recognizer.SetInputToDefaultAudioDevice();

            Choices choices = new Choices("yes", "no");

            for (int i = 0; i < words.Length; i++)
            {
                words_copy[i] = words[i];
                words[i] = textBox2.Text + " " + words[i];
                choices.Add(words[i]);

            }

            GrammarBuilder grBuilder = new GrammarBuilder(choices);
            Grammar grammar = new Grammar(grBuilder);
            _recognizer.LoadGrammar(grammar);

            _recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);
            _recognizer.RecognizeAsync(RecognizeMode.Multiple);
        }

        void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (voiceCommandsActive)
            {
                if (e.Result.Text == words[7])
                {
                    if (explorer)
                    {
                        if (e.Result.Confidence > conf && confirmation.IsVisible == false)
                        {
                            System.Diagnostics.Process.Start("IEXPLORE.EXE");
                        }
                    }
                }


                if (e.Result.Text == words[6])
                {
                    if (firefox)
                    {
                        if (e.Result.Confidence > conf && confirmation.IsVisible == false)
                        {
                            System.Diagnostics.Process.Start("FIREFOX.EXE");
                        }
                    }
                }

                if (e.Result.Text == "yes")
                {
                    if (e.Result.Confidence > conf && confirmation.IsVisible == true)
                    {
                        confirmation.Yes_Click(sender, e);
                    }
                }

                if (e.Result.Text == "no")
                {
                    if (e.Result.Confidence > conf && confirmation.IsVisible == true)
                    {
                        confirmation.No_Click(sender, e);
                    }
                }

                if (e.Result.Text == words[0])
                {
                    if (_logoff)
                    {

                        confirmation.command = words_copy[0];
                        confirmation.label1.Content = "Are you sure you want to " + words_copy[0] + "?";
                        logoff(e.Result.Confidence);
                    }
                }

                if (e.Result.Text == words[1])
                {
                    if (_shutdown)
                    {

                        confirmation.command = words_copy[1];
                        confirmation.label1.Content = "Are you sure you want to " + words_copy[1] + "?";
                        shutDown(e.Result.Confidence);
                    }
                }

                if (e.Result.Text == words[2])
                {
                    if (_restart)
                    {
                        confirmation.command = words_copy[2];
                        confirmation.label1.Content = "Are you sure you want to " + words_copy[2] + "?";
                        restart(e.Result.Confidence);
                    }
                }

                if (e.Result.Text == words[3])
                {
                    if (_Hibernate)
                    {
                        confirmation.command = words_copy[3];
                        confirmation.label1.Content = "Are you sure you want to " + words_copy[3] + "?";
                        Hibernate(e.Result.Confidence);
                    }
                }

                if (e.Result.Text == words[4])
                {
                    if (e.Result.Confidence > conf && confirmation.IsVisible == false)
                    {
                        textBox1.Text = "deactivated";
                        voiceCommandsActive = false;
                    }
                }
            }
            if (e.Result.Text == words[5])
            {
                if (e.Result.Confidence > conf && confirmation.IsVisible == false)
                {
                    textBox1.Text = "activated";
                    voiceCommandsActive = true;
                }
            }


        }

        void logoff(float newConf)
        {
            if (newConf >= conf && confirmation.IsVisible == false)
            {
                confirmation.Show();
            }
        }

        void shutDown(float newConf)
        {
            if (newConf >= conf && confirmation.IsVisible == false)
            {
                confirmation.Show();
            }
        }

        void restart(float newConf)
        {
            if (newConf >= conf && confirmation.IsVisible == false)
            {
                confirmation.Show();
            }
        }

        void Hibernate(float newConf)
        {
            if (newConf >= conf && confirmation.IsVisible == false)
            {
                confirmation.Show();
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        
    }
}